import { Component, Injector, ViewChild, ViewEncapsulation, ElementRef } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ClassificationServiceProxy, ClassificationListDto } from '@shared/service-proxies/service-proxies';
import { Paginator } from 'primeng/components/paginator/paginator';
import { CreateOrEditClassificationModalComponent } from './create-or-edit-classification-modal.component';

@Component({
  templateUrl: './classification.component.html',
  styleUrls: ['./classification.component.less'],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})

export class ClassificationComponent extends AppComponentBase {
  @ViewChild('classificationsTable') classificationsTable: ElementRef;
  @ViewChild('createOrEditClassificationModal') createOrEditClassificationModal: CreateOrEditClassificationModalComponent;
  @ViewChild('paginator') paginator: Paginator;

  classifications: Array<object>;

  constructor(
    injector: Injector,
    private _classificationService: ClassificationServiceProxy,
  ) {
    super(injector);
  }

  // tslint:disable-next-line:use-life-cycle-interface
  ngOnInit() {
    this.getClassifications();
  }

  getClassifications() {
    this._classificationService.getTreeClassifications().subscribe(result => {
      this.classifications = result.items;
    });
  }

  deleteClassification(classification: ClassificationListDto): void {
    this.message.confirm(
      '确定要删除分类(' + classification.displayName + ')',
      isConfirmed => {
        if (isConfirmed) {
          this._classificationService.deleteClassification(classification.id).subscribe(() => {
            this.getClassifications();
            this.notify.success(this.l('SuccessfullyDeleted'));
          });
        }
      }
    );
  }
}
